home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swags_z.zip / TIMING.SWG / 0007_High Resolution Timer.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  6KB  |  164 lines

  1. {$S-,R-,I-,V-,B-}
  2.  
  3. {*********************************************************}
  4. {*                   TPTIMER.PAS 2.00                    *}
  5. {*                by TurboPower Software                 *}
  6. {*********************************************************}
  7.  
  8. Unit TpTimer;
  9.   {-Allows events to be timed With 1 microsecond resolution}
  10.  
  11.  
  12.  
  13. Interface
  14. Const
  15.   TimerResolution = 1193181.667;
  16. Procedure InitializeTimer;
  17.   {-ReProgram the timer chip to allow 1 microsecond resolution}
  18.  
  19. Procedure RestoreTimer;
  20.   {-Restore the timer chip to its normal state}
  21.  
  22. Function ReadTimer : LongInt;
  23.   {-Read the timer With 1 microsecond resolution}
  24.  
  25. Function ElapsedTime(Start, Stop : LongInt) : Real;
  26.   {-Calculate time elapsed (in milliseconds) between Start and Stop}
  27.  
  28. Function ElapsedTimeString(Start, Stop : LongInt) : String;
  29.   {-Return time elapsed (in milliseconds) between Start and Stop as a String}
  30.  
  31.   {==========================================================================}
  32.  
  33. Implementation
  34.  
  35. Var
  36.   SaveExitProc : Pointer;
  37.   Delta : LongInt;
  38.  
  39.   Function Cardinal(L : LongInt) : Real;
  40.     {-Return the unsigned equivalent of L as a Real}
  41.   begin                      {Cardinal}
  42.     if L < 0 then
  43.       Cardinal := 4294967296.0+L
  44.     else
  45.       Cardinal := L;
  46.   end;                       {Cardinal}
  47.  
  48.   Function ElapsedTime(Start, Stop : LongInt) : Real;
  49.     {-Calculate time elapsed (in milliseconds) between Start and Stop}
  50.   begin                      {ElapsedTime}
  51.     ElapsedTime := 1000.0*Cardinal(Stop-(Start+Delta))/TimerResolution;
  52.   end;                       {ElapsedTime}
  53.  
  54.   Function ElapsedTimeString(Start, Stop : LongInt) : String;
  55.     {-Return time elapsed (in milliseconds) between Start and Stop as a String}
  56.   Var
  57.     R : Real;
  58.     S : String;
  59.   begin                      {ElapsedTimeString}
  60.     R := ElapsedTime(Start, Stop);
  61.     Str(R:0:3, S);
  62.     ElapsedTimeString := S;
  63.   end;                       {ElapsedTimeString}
  64.  
  65.   Procedure InitializeTimer;
  66.     {-ReProgram the timer chip to allow 1 microsecond resolution}
  67.   begin                      {InitializeTimer}
  68.     {select timer mode 2, read/Write channel 0}
  69.     Port[$43] := $34;        {00110100b}
  70.     Inline($EB/$00);         {jmp short $+2 ;Delay}
  71.     Port[$40] := $00;        {LSB = 0}
  72.     Inline($EB/$00);         {jmp short $+2 ;Delay}
  73.     Port[$40] := $00;        {MSB = 0}
  74.   end;                       {InitializeTimer}
  75.  
  76.   Procedure RestoreTimer;
  77.     {-Restore the timer chip to its normal state}
  78.   begin                      {RestoreTimer}
  79.     {select timer mode 3, read/Write channel 0}
  80.     Port[$43] := $36;        {00110110b}
  81.     Inline($EB/$00);         {jmp short $+2 ;Delay}
  82.     Port[$40] := $00;        {LSB = 0}
  83.     Inline($EB/$00);         {jmp short $+2 ;Delay}
  84.     Port[$40] := $00;        {MSB = 0}
  85.   end;                       {RestoreTimer}
  86.  
  87.   Function ReadTimer : LongInt;
  88.     {-Read the timer With 1 microsecond resolution}
  89.   begin                      {ReadTimer}
  90.     Inline(
  91.       $FA/                   {cli             ;Disable interrupts}
  92.       $BA/$20/$00/           {mov  dx,$20     ;Address PIC ocw3}
  93.       $B0/$0A/               {mov  al,$0A     ;Ask to read irr}
  94.       $EE/                   {out  dx,al}
  95.       $B0/$00/               {mov  al,$00     ;Latch timer 0}
  96.       $E6/$43/               {out  $43,al}
  97.       $EC/                   {in   al,dx      ;Read irr}
  98.       $89/$C7/               {mov  di,ax      ;Save it in DI}
  99.       $E4/$40/               {in   al,$40     ;Counter --> bx}
  100.       $88/$C3/               {mov  bl,al      ;LSB in BL}
  101.       $E4/$40/               {in   al,$40}
  102.       $88/$C7/               {mov  bh,al      ;MSB in BH}
  103.       $F7/$D3/               {not  bx         ;Need ascending counter}
  104.       $E4/$21/               {in   al,$21     ;Read PIC imr}
  105.       $89/$C6/               {mov  si,ax      ;Save it in SI}
  106.       $B0/$FF/               {mov  al,$0FF    ;Mask all interrupts}
  107.       $E6/$21/               {out  $21,al}
  108.       $B8/$40/$00/           {mov  ax,$40     ;read low Word of time}
  109.       $8E/$C0/               {mov  es,ax      ;from BIOS data area}
  110.       $26/$8B/$16/$6C/$00/   {mov  dx,es:[$6C]}
  111.       $89/$F0/               {mov  ax,si      ;Restore imr from SI}
  112.       $E6/$21/               {out  $21,al}
  113.       $FB/                   {sti             ;Enable interrupts}
  114.       $89/$F8/               {mov  ax,di      ;Retrieve old irr}
  115.       $A8/$01/               {test al,$01     ;Counter hit 0?}
  116.       $74/$07/               {jz   done       ;Jump if not}
  117.       $81/$FB/$FF/$00/       {cmp  bx,$FF     ;Counter > $FF?}
  118.       $77/$01/               {ja   done       ;Done if so}
  119.       $42/                   {inc  dx         ;else count int req.}
  120.       {done:}
  121.       $89/$5E/$FC/           {mov [bp-4],bx   ;set Function result}
  122.       $89/$56/$FE);          {mov [bp-2],dx}
  123.   end;                       {ReadTimer}
  124.  
  125.   Procedure Calibrate;
  126.     {-Calibrate the timer}
  127.   Const
  128.     Reps = 1000;
  129.   Var
  130.     I : Word;
  131.     L1, L2, Diff : LongInt;
  132.   begin                      {Calibrate}
  133.     Delta := MaxInt;
  134.     For I := 1 to Reps do begin
  135.       L1 := ReadTimer;
  136.       L2 := ReadTimer;
  137.       {use the minimum difference}
  138.       Diff := L2-L1;
  139.       if Diff < Delta then
  140.         Delta := Diff;
  141.     end;
  142.   end;                       {Calibrate}
  143.  
  144.   {$F+}
  145.   Procedure OurExitProc;
  146.     {-Restore timer chip to its original state}
  147.   begin                      {OurExitProc}
  148.     ExitProc := SaveExitProc;
  149.     RestoreTimer;
  150.   end;                       {OurExitProc}
  151.   {$F-}
  152.  
  153. begin
  154.   {set up our Exit handler}
  155.   SaveExitProc := ExitProc;
  156.   ExitProc := @OurExitProc;
  157.  
  158.   {reProgram the timer chip}
  159.   InitializeTimer;
  160.  
  161.   {adjust For speed of machine}
  162.   Calibrate;
  163. end.
  164.